package com.heima.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.crm.common.contents.CommonMessageContent;
import com.heima.crm.common.result.PageResult;
import com.heima.crm.common.result.Result;
import com.heima.crm.common.utils.BaseContext;
import com.heima.crm.mapper.CrRoleDetailsMapper;
import com.heima.crm.mapper.CrRoleMapper;
import com.heima.crm.mapper.CrUserMapper;
import com.heima.crm.pojos.dtos.RoleDetailsDto;
import com.heima.crm.pojos.dtos.RolePageDto;
import com.heima.crm.pojos.dtos.RoleSaveDto;
import com.heima.crm.pojos.dtos.UpdateRoleDto;
import com.heima.crm.pojos.entity.TbRole;
import com.heima.crm.pojos.entity.TbRoleDetails;
import com.heima.crm.pojos.entity.TbUser;
import com.heima.crm.pojos.vos.CrRoleVo;
import com.heima.crm.service.CrRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Transactional
@Service
public class CrRoleServiceImpl implements CrRoleService {
    @Resource
    private CrRoleMapper roleMapper;
    @Resource
    private CrRoleDetailsMapper crRoleDetailsMapper;
    @Resource
    private CrUserMapper crUserMapper;

    //分页
    @Override
    public Result page(RolePageDto dto) {
        if (dto==null){
            return Result.error(CommonMessageContent.INVALID_PARAMETER);
        }
        dto.checkParam();
        LambdaQueryWrapper<TbRole> lqw =new LambdaQueryWrapper<>();
        if (dto.getRoleName()!=null && !"".equals(dto.getRoleName())){
            lqw.like(TbRole::getRoleName,dto.getRoleName());
        }
        if (dto.getStatus()!=null){
            lqw.eq(TbRole::getStatus,dto.getStatus());
        }
        lqw.orderByAsc(TbRole::getSort);

        Page page =new Page(dto.getPageNum() ,dto.getPageSize());
        page = roleMapper.selectPage(page,lqw);
        PageResult pageResult =new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getRecords());
        return Result.success(pageResult);
    }
    //添加角色

    @Override
    public Result saveRole(RoleSaveDto dto) {
        if (dto==null){
            return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        }
        TbRole tbRole = buildRole(dto);
        roleMapper.insert(tbRole);
        if (dto.getList()!=null && dto.getList().size()>0){
            for (TbRoleDetails roleDetailsDto : dto.getList()) {
                roleDetailsDto.setRoleId(tbRole.getId());
                crRoleDetailsMapper.insert(roleDetailsDto);
            }
        }

        return Result.success();
    }


    private static TbRole buildRole(RoleSaveDto roleSaveDto) {
        TbRole tbRole = new TbRole();
        BeanUtils.copyProperties(roleSaveDto,tbRole);
        tbRole.setCreateId(BaseContext.getCurrentId());
        tbRole.setCreateTime(LocalDateTime.now());
        tbRole.setUpdateId(BaseContext.getCurrentId());
        tbRole.setUpdateTime(LocalDateTime.now());
        return tbRole;
    }

    @Override
    public Result getRole(Long roleId) {
        if (roleId==null) return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        TbRole tbRole = roleMapper.selectOne(new LambdaQueryWrapper<TbRole>().eq(TbRole::getId, roleId));
        if (tbRole==null) return Result.error(CommonMessageContent.ROLE_DOES_NOT_EXIST);
        //List<TbRoleDetails> tbRoleDetails = crRoleDetailsMapper.selectList(new LambdaQueryWrapper<TbRoleDetails>().eq(TbRoleDetails::getRoleId, roleId));
        List<TbRoleDetails> tbRoleDetails = crRoleDetailsMapper.selectRoleDetails(roleId);
        CrRoleVo crRoleVo = new CrRoleVo();
        crRoleVo.setTbRole(tbRole);
        crRoleVo.setList(tbRoleDetails);
        return Result.success(crRoleVo);
    }

    @Override
    public Result updateRole(UpdateRoleDto updateRoleDto) {
        if (updateRoleDto==null) return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        updateRoleDto.setUpdateId(BaseContext.getCurrentId());
        updateRoleDto.setUpdateTime(LocalDateTime.now());
        TbRole tbRole =new TbRole();
        BeanUtils.copyProperties(updateRoleDto,tbRole);
        roleMapper.updateById(tbRole);
        Integer id = tbRole.getId();
        crRoleDetailsMapper.delete(new LambdaQueryWrapper<TbRoleDetails>().eq(TbRoleDetails::getRoleId,id));
        if (updateRoleDto.getList()!=null && updateRoleDto.getList().size()>0){
            for (TbRoleDetails tbRoleDetails : updateRoleDto.getList()) {
                crRoleDetailsMapper.insert(tbRoleDetails);
            }
        }

        return Result.success();
    }

    @Override
    public Result deleteRoleById(Long roleId) {
        if (roleId==null) return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        Integer integer = Math.toIntExact(crUserMapper.selectCount(new LambdaQueryWrapper<TbUser>().eq(TbUser::getUserType, roleId)));
        if (integer>0) return Result.error("角色被使用了不能删除");
        List<TbRoleDetails> tbRoleDetails = crRoleDetailsMapper.selectList(new LambdaQueryWrapper<TbRoleDetails>().eq(TbRoleDetails::getRoleId, roleId));
        if (tbRoleDetails!=null && tbRoleDetails.size()>0){
            int delete = crRoleDetailsMapper.delete(new LambdaQueryWrapper<TbRoleDetails>().eq(TbRoleDetails::getRoleId, roleId));
            if (delete == 0) return Result.error(CommonMessageContent.DELETE_ERROR);
        }
        int delete1 = roleMapper.deleteById(roleId);
        if (delete1==0) return Result.error(CommonMessageContent.DELETE_ERROR);
        return Result.success("删除成功");
    }


}
